package com.ruoyi.platform.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.platform.domain.CouponManagement;
import com.ruoyi.platform.mapper.CouponManagementMapper;
import com.ruoyi.platform.service.ICouponManagementService;

/**
 * 优惠券管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-01-01
 */
@Service
public class CouponManagementServiceImpl implements ICouponManagementService {

    @Autowired
    private CouponManagementMapper couponManagementMapper;

    /**
     * 查询优惠券管理
     *
     * @param couponId 优惠券ID
     * @return CouponManagement
     */
    @Override
    public CouponManagement selectCouponManagementByCouponId(String couponId) {
        return couponManagementMapper.selectCouponManagementByCouponId(couponId);
    }

    /**
     * 查询优惠券管理列表
     *
     * @param couponManagement 查询条件
     * @return 列表
     */
    @Override
    public List<CouponManagement> selectCouponManagementList(CouponManagement couponManagement) {
        return couponManagementMapper.selectCouponManagementList(couponManagement);
    }

    /**
     * 新增优惠券管理
     *
     * @param couponManagement 实体
     * @return 结果
     */
    @Override
    public int insertCouponManagement(CouponManagement couponManagement) {
        // 若主键为空，生成UUID
        if (couponManagement.getCouponId() == null || couponManagement.getCouponId().trim().isEmpty()) {
            couponManagement.setCouponId(IdUtils.fastSimpleUUID());
        }
        return couponManagementMapper.insertCouponManagement(couponManagement);
    }

    /**
     * 修改优惠券管理
     *
     * @param couponManagement 实体
     * @return 结果
     */
    @Override
    public int updateCouponManagement(CouponManagement couponManagement) {
        return couponManagementMapper.updateCouponManagement(couponManagement);
    }

    /**
     * 批量删除优惠券管理
     *
     * @param couponIds ids
     * @return 结果
     */
    @Override
    public int deleteCouponManagementByCouponIds(String[] couponIds) {
        return couponManagementMapper.deleteCouponManagementByCouponIds(couponIds);
    }

    /**
     * 删除优惠券管理信息
     *
     * @param couponId id
     * @return 结果
     */
    @Override
    public int deleteCouponManagementByCouponId(String couponId) {
        return couponManagementMapper.deleteCouponManagementByCouponId(couponId);
    }

    /**
     * 审核通过优惠券
     */
    @Override
    public int approveCoupon(String couponId, Long approvalBy, String approvalRemark) {
        CouponManagement cm = new CouponManagement();
        cm.setCouponId(couponId);
        cm.setApprovalStatus(1); // 1-审核通过
        cm.setStatus(1); // 1-审核通过 (根据表设计自行调整)
        cm.setApprovalBy(approvalBy);
        cm.setApprovalRemark(approvalRemark);
        cm.setApprovalAt(new Date());
        return couponManagementMapper.updateCouponApprovalStatus(cm);
    }

    /**
     * 拒绝优惠券
     */
    @Override
    public int rejectCoupon(String couponId, Long approvalBy, String rejectReason) {
        CouponManagement cm = new CouponManagement();
        cm.setCouponId(couponId);
        cm.setApprovalStatus(2); // 2-审核拒绝
        cm.setStatus(2); // 2-审核拒绝/根据业务需求调整
        cm.setApprovalBy(approvalBy);
        cm.setRejectReason(rejectReason);
        cm.setApprovalAt(new Date());
        return couponManagementMapper.updateCouponApprovalStatus(cm);
    }

    /**
     * 根据商家ID查询优惠券列表
     */
    @Override
    public List<CouponManagement> selectCouponManagementByMerchantId(String merchantId) {
        return couponManagementMapper.selectCouponManagementByMerchantId(merchantId);
    }

    /**
     * 根据审核状态查询优惠券列表
     */
    @Override
    public List<CouponManagement> selectCouponManagementByApprovalStatus(Integer approvalStatus) {
        return couponManagementMapper.selectCouponManagementByApprovalStatus(approvalStatus);
    }
}